
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */


package oxygen.tool.wlfacade.helpers;

import java.util.Map;

import javax.management.ObjectName;

import oxygen.tool.wlfacade.WLFacadeContextHelper;
import oxygen.tool.wlfacade.WLMBSTree;
import oxygen.tool.wlfacade.WLScriptHelper;
import oxygen.util.OxyTable;
import oxygen.util.StringUtils;

public class WLMisc {
  
  public static ObjectName objname(WLFacadeContextHelper wlctx) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    WLMBSTree wltree = (WLMBSTree)wlctx.getCurrentTree();
    return (ObjectName)wltree.pointer();
  }

  public static ObjectName objname(WLFacadeContextHelper wlctx, String stringrep) throws Exception {
    WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
    ObjectName cmo = WLScriptHelper.getObjName(wlctx, stringrep);
    return cmo;
  }

  public static boolean exists(WLFacadeContextHelper wlctx, String stringrep) {
    try {
      WLScriptHelper.ensureConnected(wlctx);
    WLScriptHelper.ensureMBSTree(wlctx);
      ObjectName cmo = WLScriptHelper.getObjName(wlctx, stringrep);
      if(cmo != null) {
        return true;
      }
    } catch (Exception exc) { }
    return false;
  }
  
  public static String getprompt(WLFacadeContextHelper wlctx) throws Exception {
    return wlctx.getPrompt();
  }
  
  public static Throwable lastexception(WLFacadeContextHelper wlctx) throws Exception {
    return wlctx.getFacadeContext().getLastThrowable();
  }
  
  public static String stacktrace(WLFacadeContextHelper wlctx) throws Exception {
    return stacktrace(wlctx, wlctx.getFacadeContext().getLastThrowable());
  }

  public static String stacktrace(WLFacadeContextHelper wlctx, Throwable thr) throws Exception {
    return StringUtils.toString(thr);
  }
  
  public static String version(WLFacadeContextHelper wlctx) throws Exception {
    return weblogic.version.getVersions();
  }

  public static String versionNumber(WLFacadeContextHelper wlctx) throws Exception {
    return (weblogic.version.getPLInfo())[1];
  }
  
  public static OxyTable totable(WLFacadeContextHelper wlctx, Object obj) throws Exception {
    return OxyTable.parseFrom(obj);
  }

  public static Map tomap(WLFacadeContextHelper wlctx, Object obj) throws Exception {
    return WLScriptHelper.toMap(obj);
  }
  
}

